# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

from wlmodels.codes.dicts import ex02_dict, companytype_dict
from wlmodels.codes.fields import CrossForeignCharField
from wlmodels.codes.models import ELBaseModel, ELRegion, ELNic


# 各个表的统计
class ELCount(models.Model):
    model = models.CharField('模型', max_length=30, null=True, blank=True)
    table = models.CharField('数据表', max_length=30, null=True, blank=True)
    name = models.CharField('表名', max_length=30, null=True, blank=True)
    count = models.IntegerField('总数', null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True, auto_now=True)

    class Meta:
        verbose_name = '数据表汇总'
        verbose_name_plural = '数据表汇总'
        app_label = "dataplus"
        db_table = 'statistics_count'

    def __unicode__(self):
        return u'%s' % self.id


# 企业分类统计抽象类
class ELStatisticsEnt(ELBaseModel):
    tmonth = models.SmallIntegerField('月份', null=True, blank=True)
    region_id_id = CrossForeignCharField('所在地区', db_column="region_id", max_length=6, blank=True)
    nic_id_id = CrossForeignCharField('行业分类', db_column="nic_id", max_length=5, blank=True)
    companytype = models.BooleanField('企业类型', blank=True, choices=companytype_dict, default=0)
    ENTSTATUS = models.CharField('企业状态', max_length=1, blank=True, choices=ex02_dict)
    count = models.IntegerField('计数', null=True, blank=True)
    companyage = models.TextField('企业年龄分布', null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True, auto_now=True)

    class Meta:
        verbose_name = '企业分类统计'
        verbose_name_plural = '企业分类统计'
        app_label = "dataplus"
        db_table = 'statistics_ent'

    def __unicode__(self):
        return u'%s' % self.id

    def _region_id(self):
        return ELRegion.get_region(self.region_id_id, m="obj")

    _region_id.short_description = "所在地区"

    region_id = property(_region_id)

    def _nic_id(self):
        the_nic = ELNic.objects.filter(code=self.nic_id_id).first()
        if the_nic:
            return the_nic
        return ""

    _nic_id.short_description = "行业分类"

    nic_id = property(_nic_id)


# 企业月度统计表
class ELStatisticsEntMonth(ELBaseModel):
    region_id_id = CrossForeignCharField('所在地区', db_column="region_id", max_length=6, blank=True)
    nic_id_id = CrossForeignCharField('行业分类', db_column="nic_id", max_length=5, blank=True)
    tmonth = models.SmallIntegerField('月份', null=True, blank=True)
    newent = models.IntegerField('新增企业数', null=True, blank=True)
    cancelent = models.IntegerField('注销企业数', null=True, blank=True)
    revokeent = models.IntegerField('吊销企业数', null=True, blank=True)
    newgeti = models.IntegerField('新增个体数', null=True, blank=True)
    cancelgeti = models.IntegerField('注销个体数', null=True, blank=True)
    revokegeti = models.IntegerField('吊销个体数', null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True, auto_now=True)

    class Meta:
        verbose_name = '企业月度统计'
        verbose_name_plural = '企业月度统计'
        app_label = "dataplus"
        db_table = 'statistics_ent_month'

    def __unicode__(self):
        return u'%s' % self.id

    def _region_id(self):
        return ELRegion.get_region(self.region_id_id, m="obj")

    _region_id.short_description = "所在地区"

    region_id = property(_region_id)

    def _nic_id(self):
        the_nic = ELNic.objects.filter(code=self.nic_id_id).first()
        if the_nic:
            return the_nic
        return ""

    _nic_id.short_description = "行业分类"

    nic_id = property(_nic_id)


# 税务年度统计表
class ELStatisticsTax(ELBaseModel):
    region_id_id = CrossForeignCharField('所在地区', db_column="region_id", max_length=6, blank=True)
    nic_id_id = CrossForeignCharField('行业分类', db_column="nic_id", max_length=5, blank=True)
    year = models.SmallIntegerField('所属年度', choices=((2012, 2012), (2013, 2013), (2014, 2014), (2015, 2015), (2016, 2016),),
                                    null=True, db_index=True)
    revenue = models.BigIntegerField('平均营业收入', null=True, blank=True)
    revenue_total = models.BigIntegerField('总营业收入', null=True, blank=True)
    total_profit = models.BigIntegerField('平均利润总额', null=True, blank=True)
    total_profit_total = models.BigIntegerField('总利润总额', null=True, blank=True)
    assets = models.BigIntegerField('平均资产总额', null=True, blank=True)
    assets_total = models.BigIntegerField('总资产总额', null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True, auto_now=True)

    class Meta:
        verbose_name = '税务年度统计'
        verbose_name_plural = '税务年度统计'
        app_label = "dataplus"
        db_table = 'statistics_tax'

    def __unicode__(self):
        return u'%s' % self.id

    def _region_id(self):
        return ELRegion.get_region(self.region_id_id, m="obj")

    _region_id.short_description = "所在地区"

    region_id = property(_region_id)

    def _nic_id(self):
        the_nic = ELNic.objects.filter(code=self.nic_id_id).first()
        if the_nic:
            return the_nic
        return ""

    _nic_id.short_description = "行业分类"

    nic_id = property(_nic_id)


# 进出口月度统计表
class ELStatisticsHG(ELBaseModel):
    tmonth = models.SmallIntegerField('月份', null=True, blank=True)
    region_id_id = CrossForeignCharField('所在地区', db_column="region_id", max_length=6, blank=True)
    nic_id_id = CrossForeignCharField('行业分类', db_column="nic_id", max_length=5, blank=True)
    timport = models.BigIntegerField('进口总额', null=True, blank=True)
    texport = models.BigIntegerField('出口总额', null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True, auto_now=True)

    class Meta:
        verbose_name = '进出口月度统计'
        verbose_name_plural = '进出口月度统计'
        app_label = "dataplus"
        db_table = 'statistics_hg'

    def __unicode__(self):
        return u'%s' % self.id

    def _region_id(self):
        return ELRegion.get_region(self.region_id_id, m="obj")

    _region_id.short_description = "所在地区"

    region_id = property(_region_id)

    def _nic_id(self):
        the_nic = ELNic.objects.filter(code=self.nic_id_id).first()
        if the_nic:
            return the_nic
        return ""

    _nic_id.short_description = "行业分类"

    nic_id = property(_nic_id)


# 信用评分地区行业统计
class ELStatisticsCredit(ELBaseModel):
    year = models.SmallIntegerField('评价日期', null=True)
    region_name = models.CharField('所在地区', max_length=50, null=True, blank=True)
    nic_name = models.CharField('行业分类', max_length=50, null=True, blank=True)
    index_total = models.SmallIntegerField('平均总分', null=True, blank=True)

    class Meta:
        verbose_name = '地区行业信用统计'
        verbose_name_plural = '地区行业信用统计'
        app_label = "dataplus"
        db_table = 'statistics_credit'

    def __unicode__(self):
        return u'%s' % self.id


models_statistics_imported = 0  # 仅供主model引用的虚变量
